﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore;

namespace WhaleAccount.Models;

/// <summary>
/// 收支记录
/// </summary>
[Comment("收支记录")]
public class Record : INotifyPropertyChanged
{
    private bool isEditable = false;
    private int id;
    private int category;
    private int type;
    private double _value;
    private DateTime date;
    private string? remark;
    private string? extend;

    [Key]
    [Comment("ID")]
    public int Id
    {
        get => id;
        set
        {
            if (id != value)
            {
                id = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("分类，0：支出，1：收入")]
    public int Category
    {
        get => category;
        set
        {
            if (category != value)
            {
                category = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("类型，0：支出，1：收入")]
    public int Type
    {
        get => type;
        set
        {
            if (type != value)
            {
                type = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("金额")]
    public double Value
    {
        get => _value;
        set
        {
            if (_value != value)
            {
                _value = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("日期")]
    public DateTime Date
    {
        get => date;
        set
        {
            if (date != value)
            {
                date = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("备注")]
    public string? Remark
    {
        get => remark;
        set
        {
            if (remark != value)
            {
                remark = value;
                OnPropertyChanged();
            }
        }
    }
    [Comment("扩展字段")]
    public string? Extend
    {
        get => extend;
        set
        {
            if (extend != value)
            {
                extend = value;
                OnPropertyChanged();
            }
        }
    }
    [NotMapped]
    public bool IsEditable
    {
        get => isEditable;
        set
        {
            if (isEditable != value)
            {
                isEditable = value;
                OnPropertyChanged();
            }
        }
    }

    public Record()
    {

    }

    public Record(int category, int type, float value, DateTime date)
    {
        Category = category;
        Type = type;
        Value = value;
        Date = date;
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        // Raise the PropertyChanged event, passing the name of the property whose value has changed.
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
